package com.ruoyi.baseData.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.baseData.domain.EquipmentConfig;
import com.ruoyi.baseData.domain.EquipmentLevel;
import com.ruoyi.baseData.domain.Material;
import com.ruoyi.baseData.domain.dto.EquipmentConfigDto;
import com.ruoyi.baseData.domain.vo.EquipmentConfigVo;
import com.ruoyi.baseData.mapper.EquipmentConfigMapper;
import com.ruoyi.baseData.mapper.EquipmentLevelMapper;
import com.ruoyi.baseData.service.EquipmentConfigService;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;

import com.ruoyi.common.core.utils.sql.QueryHelp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
*
*/
@Service
public class EquipmentConfigServiceImpl implements EquipmentConfigService {

    @Resource
    EquipmentConfigMapper equipmentConfigMapper;

    @Resource
    EquipmentLevelMapper equipmentLevelMapper;


    @Override
    public Page<EquipmentConfigVo> getEquipmentConfigByPage(EquipmentConfigDto pageEntity) {

        MPJLambdaWrapper<EquipmentConfig> wrapper = new MPJLambdaWrapper<EquipmentConfig>()
                .selectAll(EquipmentConfig.class)//查询user表全部字段
                //记的测试阶段
                .eq(EquipmentConfig::getDelFlag, 0)
                .orderByDesc(EquipmentConfig::getCreateTime);
        wrapper = QueryHelp.getPredicateEx(wrapper, EquipmentConfig.class, pageEntity);
        Page<EquipmentConfigVo> userList = equipmentConfigMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   EquipmentConfigVo.class, wrapper);
        return userList;
    }

    @Override
    public void addEquipmentConfig(EquipmentConfigDto testDto) {
        MPJLambdaWrapper<EquipmentConfig> wrapper = new MPJLambdaWrapper<EquipmentConfig>()
                .eq(EquipmentConfig::getCode, testDto.getCode()).ne(EquipmentConfig::getDelFlag, 1);

        EquipmentConfig EquipmentConfig = equipmentConfigMapper.selectOne(wrapper);

        EquipmentLevel equipmentLevel = equipmentLevelMapper.selectById(testDto.getEquipmentLevelId());



        if(null != EquipmentConfig && null != equipmentLevel) {
            throw new GlobalException("该EquipmentConfig已经存在");
        }
        EquipmentConfig test2 = new EquipmentConfig();
        BeanUtils.copyProperties(testDto, test2, new String[]{"id"});
        test2.setEquipmentLevelId(equipmentLevel.getId());
        test2.setLevel(equipmentLevel.getLevel());
        test2.setLevelName(equipmentLevel.getName());
        test2.setLevelCode(equipmentLevel.getCode());
        test2.setDelFlag(false);
        test2.setCreateBy("admin");
        test2.setCreateTime(new Date());
        equipmentConfigMapper.insert(test2);
    }

    @Override
    public void updateEquipmentConfig(EquipmentConfigDto testDto) {
        MPJLambdaWrapper<EquipmentConfig> wrapper = new MPJLambdaWrapper<EquipmentConfig>()
                .eq(EquipmentConfig::getCode, testDto.getCode());
        EquipmentConfig EquipmentConfig = equipmentConfigMapper.selectOne(wrapper);
        if(null == EquipmentConfig) {
            throw new GlobalException("该Test不存在");
        }
        BeanUtils.copyProperties(testDto, EquipmentConfig);
        EquipmentConfig.setCreateBy("admin");
        EquipmentConfig.setCreateTime(new Date());
        equipmentConfigMapper.updateById(EquipmentConfig);
    }

    @Override
    public void delEquipmentConfig(EquipmentConfigDto testDto) {
        EquipmentConfig EquipmentConfig = equipmentConfigMapper.selectById(testDto.getId());
        if(null == EquipmentConfig) {
            throw new GlobalException("该Test不存在");
        }
        EquipmentConfig.setDelFlag(true);
        equipmentConfigMapper.updateById(EquipmentConfig);
    }
}
